const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const jsonData = require("../jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({
 
	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		leadName: '',
		addOrMod:'add',
		//选项集
		eyelidPositionIndex:-1,
		eyelidShapeIndex:-1,
		eyelashDirectionIndex:-1,
		eyelidNeoplasmsIndex:-1,
		eyelidctropionIndex:-1,
		trichiasisIndex:-1,
		blepharoptosisIndex:-1,
		eyelidNeoplasmsDiagnosisIndex:-1,
		normalOrAbnormal:jsonData.normalOrAbnormal,
		haveOrNot:jsonData.haveOrNot,
		noneOrBothOrLeftOrRight:jsonData.noneOrBothOrLeftOrRight,
    schoolId:'',
		/**眼睑眼眶检查**/
		//眼睑位置
		eyelidPosition: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0025',
			new_contentid: 'NR0029',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//眼睑位置异常说明
		positionDescription: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0025',
			new_contentid: 'NR0031',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//眼睑形状
		eyelidShape: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0026',
			new_contentid: 'NR0032',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//眼睑形状异常说明
		shapeDescription: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0026',
			new_contentid: 'NR0034',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//睫毛方向
		eyelashDirection: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0027',
			new_contentid: 'NR0035',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//睫毛方向异常说明
		eyelashDescription: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0027',
			new_contentid: 'NR0037',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//眼睑新生物
		eyelidNeoplasms: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0028',
			new_contentid: 'NR0038',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//初步诊断-眼睑外翻
		eyelidctropion: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0029',
			new_contentid: 'NR0041',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//初步诊断-睑内翻(倒睫)
		trichiasis: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0029',
			new_contentid: 'NR0043',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//初步诊断-上睑下垂
		blepharoptosis: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0029',
			new_contentid: 'NR0045',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//初步诊断-眼睑新生物
		eyelidNeoplasmsDiagnosis: {
			new_scid: null,
			new_categoryid: 'LB0010',
			new_itemid: 'XM0029',
			new_contentid: 'NR0047',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getEyelidResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
		}
	},
	//提交表单
	eyelibSubmit: function(e) {
		var createResult = this.getEyelib();
		var that = this;
		commonutil.createScreenResults(createResult,function(res){
			var idArray = res.result.split('|');
			for(var i = 0;i < idArray.length;i++){
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				eyelidPosition:	createResult[0],
				positionDescription:	createResult[1],
				eyelidShape:	createResult[2],
				shapeDescription:	createResult[3],
				eyelashDirection:	createResult[4],
				eyelashDescription:	createResult[5],
				eyelidNeoplasms:	createResult[6],
				eyelidctropion:	createResult[7],
				trichiasis:	createResult[8],
				blepharoptosis:	createResult[9],
				eyelidNeoplasmsDiagnosis:	createResult[10],
			});
			console.log(that.data.eyelidNeoplasmsDiagnosis);
		});
	},
	//更新
	eyelibUpdate:function(){
		var updateResult = this.getEyelib();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getEyelidResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title:'加载中...',
			mask:true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if(reusltArray.length==0){
			wx.hideLoading();
			return ;
		}
		this.setData({
			addOrMod:'mod'
		})
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0029') {
				this.setData({
					eyelidPosition: element
				})
			} else if (element.new_contentid === 'NR0031') {
				this.setData({
					positionDescription: element
				})
			} else if (element.new_contentid === 'NR0032') {
				//debugger;
				this.setData({
					eyelidShape: element
				})
			} else if (element.new_contentid === 'NR0034') {
				this.setData({
					shapeDescription: element
				})
			} else if (element.new_contentid === 'NR0035') {
				this.setData({
					eyelashDirection: element
				})
			}else if (element.new_contentid === 'NR0037') {
				this.setData({
					eyelashDescription: element
				})
			} else if (element.new_contentid === 'NR0038') {
				this.setData({
					eyelidNeoplasms: element
				})
			}else if (element.new_contentid === 'NR0041') {
				this.setData({
					eyelidctropion: element
				})
			} else if (element.new_contentid === 'NR0043') {
				this.setData({
					trichiasis: element
				})
			}else if (element.new_contentid === 'NR0045') {
				this.setData({
					blepharoptosis: element
				})
			} else if (element.new_contentid === 'NR0047') {
				this.setData({
					eyelidNeoplasmsDiagnosis: element
				})
			}		
		}
		wx.hideLoading();
	},
	/**选项集、文本赋值**/
	onEyelidPositionChange: function(e) {
		var result = 'eyelidPosition.new_result';
		this.setData({
			[result]: '',
			eyelidPositionIndex: e.detail.index
		})
	},
	onPositionDescriptionChange: function(e) {
		var result = 'positionDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onEyelidShapeChange: function(e) {
		var result = 'eyelidShape.new_result';
		this.setData({
			[result]: '',
			eyelidShapeIndex: e.detail.index
		})
	},
	onShapeDescriptionChange: function(e) {
		var result = 'shapeDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onEyelashDirectionIndexChange: function(e) {
		var result = 'eyelashDirection.new_result';
		this.setData({
			[result]: '',
			eyelashDirectionIndex: e.detail.index
		})
	},
	onEyelashDescriptionChange: function(e) {
		var result = 'eyelashDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onEyelidNeoplasmsChange: function(e) {
		this.setData({
			eyelidNeoplasmsIndex: e.detail.index
		})
	},
	onEyelidctropionChange: function(e) {
		this.setData({
			eyelidctropionIndex: e.detail.index
		})
	},
	onTrichiasisChange: function(e) {
		this.setData({
			trichiasisIndex: e.detail.index
		})
	},
	onBlepharoptosisChange: function(e) {
		this.setData({
			blepharoptosisIndex: e.detail.index
		})
	},
	onEyelidNeoplasmsDiagnosisChange: function(e) {
		this.setData({
			eyelidNeoplasmsDiagnosisIndex: e.detail.index
		})
	},
	
	/**获取createResult**/
	getEyelib: function() {
		var createResult = [];
		var scid = this.data.scid;
		var customerid = this.data.customerid;
		
		var eyelidPosition = this.data.eyelidPosition;
		var positionDescription = this.data.positionDescription;
		var eyelidShape = this.data.eyelidShape;
		var shapeDescription = this.data.shapeDescription;
		var eyelashDirection = this.data.eyelashDirection;
		var eyelashDescription = this.data.eyelashDescription;
		var eyelidNeoplasms = this.data.eyelidNeoplasms;
		var eyelidctropion = this.data.eyelidctropion;
		var trichiasis = this.data.trichiasis;
		var blepharoptosis = this.data.blepharoptosis;
		var eyelidNeoplasmsDiagnosis = this.data.eyelidNeoplasmsDiagnosis;
		
		//眼睑位置
		var eyelidPositionIndex = this.data.eyelidPositionIndex;
		var normalOrAbnormal = this.data.normalOrAbnormal;
		if(eyelidPositionIndex>-1){
			eyelidPosition.new_result = normalOrAbnormal[eyelidPositionIndex];		
		}	
		//眼睑形状
		var eyelidShapeIndex = this.data.eyelidShapeIndex;
		if(eyelidShapeIndex>-1){
			eyelidShape.new_result = normalOrAbnormal[eyelidShapeIndex];
		}
		//睫毛方向
		var eyelashDirectionIndex = this.data.eyelashDirectionIndex;
		if (eyelashDirectionIndex > -1) {
			eyelashDirection.new_result = normalOrAbnormal[eyelashDirectionIndex];
		}
		//眼睑新生物
		var eyelidNeoplasmsIndex = this.data.eyelidNeoplasmsIndex;
		var haveOrNot = this.data.haveOrNot;
		if (eyelidNeoplasmsIndex > -1) {
			eyelidNeoplasms.new_result = haveOrNot[eyelidNeoplasmsIndex];
		} 
		//眼睑外翻
		var eyelidctropionIndex = this.data.eyelidctropionIndex;
		var noneOrBothOrLeftOrRight = this.data.noneOrBothOrLeftOrRight;
		if (eyelidctropionIndex > -1) {
			eyelidctropion.new_result = noneOrBothOrLeftOrRight[eyelidctropionIndex];
		} 
		//睑内翻(倒睫)
		var trichiasisIndex = this.data.trichiasisIndex;
		if (trichiasisIndex>-1) {
			trichiasis.new_result = noneOrBothOrLeftOrRight[trichiasisIndex];
		} 
		//上睑下垂
		var blepharoptosisIndex = this.data.blepharoptosisIndex;
		if (blepharoptosisIndex >-1) {
			blepharoptosis.new_result = noneOrBothOrLeftOrRight[blepharoptosisIndex];
		} 
		//初步诊断-眼睑新生物
		var eyelidNeoplasmsDiagnosisIndex = this.data.eyelidNeoplasmsDiagnosisIndex;
		if (eyelidNeoplasmsDiagnosisIndex >-1) {
			eyelidNeoplasmsDiagnosis.new_result = noneOrBothOrLeftOrRight[eyelidNeoplasmsDiagnosisIndex];
		} 

		//把对象放入数组
		createResult.push(eyelidPosition);
		if(eyelidPosition.new_result=='SC0000000005'&&positionDescription.new_result!=''){
			createResult.push(positionDescription);
		}		
		createResult.push(eyelidShape);
		if(eyelidShape.new_result=='SC0000000005'&&shapeDescription.new_result!=''){
			createResult.push(shapeDescription);
		}	
		createResult.push(eyelashDirection);
		if(eyelashDirection.new_result=='SC0000000005'&&eyelashDescription.new_result!=''){
			createResult.push(eyelashDescription);
		}	
		createResult.push(eyelidNeoplasms);
		createResult.push(eyelidctropion);
		createResult.push(trichiasis);
		createResult.push(blepharoptosis);
		createResult.push(eyelidNeoplasmsDiagnosis);
		
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		console.log(createResult);
		return createResult;
	}
})
